<?php
namespace Home\Controller;
use Think\Controller;

/*
    快速链接
*/
class QuicklinkController extends InitController {

	
	//快速链接列表
	public function quick_link_list(){
		
		$result =  M('QuickLink')->where('uid = %d',$this->uid)
					  ->limit('0,10')
					  ->order('id desc')
					  ->field('id,title,link')
					  ->select();
		
		$this->assign(array(
			'result'=>$result
		));
		
		$this->display('quick_link_list');
	}
	
	//快速ajax链接
	public function ajax_quick_link_list(){
		if(IS_AJAX){
			$page  = I('page',1,'intval');
			
			$limit = $page * 10;
			
			$ajax_return = M('QuickLink')->where('uid = %d',$this->uid)
						   ->limit(''.$limit.',10')
					       ->order('id desc')
						   ->field('id,title,link')
						   ->select();
			
			if(!empty($ajax_return)){
		
				foreach($ajax_return as $k=>$v){
					$ajax_return[$k]['url'] = U('create_quick_link',array('link_id'=>$v['id']));
				}
				
				$this->ajaxReturn(array('status'=>1,'errmsg'=>'数据返回成功','data'=>$ajax_return));
			}else{
				$this->ajaxReturn(array('status'=>0,'errmsg'=>'数据返回失败'));
			}
			
		}
	}
	
	//快速ajax链接
	public function ajax_article_quick_link_list(){
		
		if(IS_AJAX){
			
			$ajax_return = M('QuickLink')->where('uid = %d',$this->uid)
					       ->order('id desc')
						   ->field('id,title,link')
						   ->select();
			
			if(!empty($ajax_return)){
		
				foreach($ajax_return as $k=>$v){
					$ajax_return[$k]['url'] = U('create_quick_link',array('link_id'=>$v['id']));
				}
				
				$this->ajaxReturn(array('status'=>1,'errmsg'=>'数据返回成功','data'=>$ajax_return));
			}else{
				$this->ajaxReturn(array('status'=>0,'errmsg'=>'数据返回失败'));
			}
			
		}
	}
	
	
	//删除快速链接
	public function ajax_delete_link(){
		if(IS_AJAX){
			$delete_id = I('delete_id');
			//删除
			if(empty($delete_id)){
				return false;
			}
			
			$return_id = M('QuickLink')->where('id = %d and uid = %d',$delete_id,$this->uid)->delete();
			if(!empty($return_id)){
				$this->success('链接删除成功',U('quick_link_list'));
			}else{
				$this->error('网络原因，链接删除失败');
			}
			
		}
	}
	
	
	//创建快速链接
	public function create_quick_link(){
		
		$id    = I('link_id','','intval');
		
		if(IS_AJAX){
			
			$title  = I('title','','trim');
			$link   = I('link','','trim');
			
			//判断标题
			if(empty($title)){
				
				$this->error('请输入链接标题');
			}
			
			if(empty($link)){
			
				$this->error('请输入链接url');
			}
			
			//非数字，非空白字符
			$judge_url = preg_match('/[^\s\d]+/', $link);

			if(!$judge_url){
				$this->error('请输入正确的网址');
			}
			
			//检测url是否加上了http;没有就加上
			$link = check_http($link);
			
			if(empty($id)){
				
				$data = array(
					'uid'=>$this->uid,
					'title'=>$title,
					'link'=>$link,
					'createtime'=>NOW_TIME,
				);
				
			}else{
				
				$data = array(
					'title'=>$title,
					'link'=>$link,
				);
			}
			
			//插入更新数据
			if(!empty($id)){
				
				$return_id = M('QuickLink')->where('id = %d and uid = %d',$id,$this->uid)->save($data);
				
			}else{
				
				$return_id = M('QuickLink')->add($data);
				
			}
			
			if($return_id !== false){
				$this->success('数据保存成功',U('quick_link_list'));
			}else{
				$this->error('网络原因，数据错误');
			}
		}
		
		if(!empty($id)){
			//返回数据
			$return = M('QuickLink')->where('id = %d',$id)->field('id,title,link')->find();
		}
		
		$this->assign(array(
			'return'=>$return
		));
		
		$this->display('create_quick_link');
	}
	
	
	

}